package org.witsoft.localmq.impl;

import org.witsoft.localmq.ConfigManager;
import org.witsoft.localmq.FileManager;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

import java.io.File;

/**
 * @ClassName: MqFileManagerImpl
 * @Description:
 * @Author: Colin
 * @Date: 2020/12/26 1:23 AM
 * @Version 1.0
 **/
@RequiredArgsConstructor
public class FileManagerImpl implements FileManager {

    @NonNull
    private ConfigManager configManager;

    public File getIndexFile(){
        File dir = configManager.getDataDir();
        File file = new File(dir, "index");
        return file;
    }

    @Override
    public File getSegmentDataFile(int id) {
        StringBuilder sb = new StringBuilder();
        int padLen = 3 - String.valueOf(id).length();
        for (int i = 0; i < padLen; i++) {
            sb.append("0");
        }
        sb.append(id);
        sb.append(".data");
        File dir = configManager.getDataDir();
        File file = new File(dir, sb.toString());
        return file;
    }

    @Override
    public File getSegmentIndexFile(int id) {
        StringBuilder sb = new StringBuilder();
        int padLen = 3 - String.valueOf(id).length();
        for (int i = 0; i < padLen; i++) {
            sb.append("0");
        }
        sb.append(id);
        sb.append(".index");
        File dir = configManager.getDataDir();
        File file = new File(dir, sb.toString());
        return file;
    }
}
